package com.mfin.bs.mappers.impl;

import com.mfin.bom.interfaces.IAddress;
import com.mfin.bom.interfaces.ICustomer;
import com.mfin.bom.interfaces.IPartner;
import com.mfin.vo.AddressVO;
import com.mfin.vo.CustomerVO;
import com.mfin.vo.PartnerVO;
import com.mfin.vo.interfaces.IMappable;

public class CustomerMapper extends BaseMapper {

	@Override
	protected boolean doMap(IMappable fromArg, IMappable toArg) {
		boolean canPerformMap = false;

		if (fromArg instanceof ICustomer && toArg instanceof CustomerVO) {
			canPerformMap = true;
			performMap((ICustomer) fromArg, (CustomerVO) toArg);
		} else if (fromArg instanceof CustomerVO && toArg instanceof ICustomer) {
			performMap((CustomerVO) fromArg, (ICustomer) toArg);
			canPerformMap = true;
		} else if (fromArg instanceof IPartner && toArg instanceof PartnerVO) {
			canPerformMap = true;
			performMap((IPartner) fromArg, (PartnerVO) toArg);
		} else if (fromArg instanceof PartnerVO && toArg instanceof IPartner) {
			performMap((PartnerVO) fromArg, (IPartner) toArg);
			canPerformMap = true;
		}
		return canPerformMap;
	}

	private void performMap(CustomerVO fromArg, ICustomer toArg) {
		if (fromArg.getCustomerId() != null) {
			toArg.setCustomerId(fromArg.getCustomerId());
		}
		toArg.setFirstName(fromArg.getFirstName());
		toArg.setMiddleName(fromArg.getMiddleName());
		toArg.setLastName(fromArg.getLastName());
		toArg.setMobile(fromArg.getMobile());
		toArg.setJoinedDate(fromArg.getJoinedDate());
	}

	private void performMap(ICustomer fromArg, CustomerVO toArg) {
		toArg.setCustomerId(fromArg.getCustomerId());
		toArg.setFirstName(fromArg.getFirstName());
		toArg.setMiddleName(fromArg.getMiddleName());
		toArg.setLastName(fromArg.getLastName());
		toArg.setMobile(fromArg.getMobile());
		toArg.setJoinedDate(fromArg.getJoinedDate());

		IAddress bomComAddress = fromArg.getCommunicationAddress();
		if (bomComAddress != null) {
			AddressVO comAddressVO = new AddressVO();
			map(bomComAddress, comAddressVO, false, false);
			toArg.setCommunicationAddress(comAddressVO);
		}

		IAddress bomPrmtAddress = fromArg.getPermanentAddress();
		if (bomPrmtAddress != null) {
			AddressVO prmtAddressVO = new AddressVO();
			map(bomPrmtAddress, prmtAddressVO, false, false);
			toArg.setPermanentAddress(prmtAddressVO);
		}
	}

	private void performMap(PartnerVO fromArg, IPartner toArg) {
		if (fromArg.getPartnerId() != null) {
			toArg.setPartnerId(fromArg.getPartnerId());
		}
	}

	private void performMap(IPartner fromArg, PartnerVO toArg) {
		if (fromArg.getPartnerId() != null) {
			toArg.setPartnerId(fromArg.getPartnerId());
		}
	}
}
